package core;

import controller.Controller;
import core.utils.Variable;
import gui.Frame;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

/**
 * Initiera model, gui osv
 */
public class VerappDesktop {
	// args[0] should be path to list of variable names/units.
	public static void main(String[] args) {
		Scanner sc = null;

		// Try to read list of variable names/units.
		try {
			sc = new Scanner(new File("res/variableNames.txt"));
		} catch (FileNotFoundException e) {
			System.err
					.println("Unable to read variable list. Check path in args[0].");
			System.exit(1);
		}
		Map<Variable, Integer> variableIds = new HashMap<Variable, Integer>();
		List<Variable> variables = new ArrayList<Variable>();
		String line;
		String[] words;
		Variable var;
		int id = 1;
		// Loop through list of variables.
		while (sc.hasNextLine()) {
			line = sc.nextLine();
			if (line.equalsIgnoreCase("EOF")){
				break;
			}
			words = line.split(" ");
			if (words.length != 2) {
				throw new ArrayIndexOutOfBoundsException(
						"List of variablenames needs to have 2 tokens per line.");
			} else {
				// Insert into appropriate list/map.
				var = new Variable(words[0], words[1]);
				//System.out.println("Main - var = " + var + " assigned to id " + id);
				variableIds.put(var, id);
				variables.add(var);
				id++;
			}
		}
		// Try to set up db-connection
		//DatabaseConnection.initialize();
		// Show GUI
		Model.getInstance().initialize(variableIds, variables);
		Controller.getInstance().initialize(new Frame());
        System.out.println("Intialized!");
	}
}
